﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using ProjectWebNhac.DTO;
using System.Data;

namespace ProjectWebNhac.GUI.User
{
    public partial class ThongTinNguoiDung : System.Web.UI.Page
    {
        NguoiDungBus ndbus = new NguoiDungBus();
        NguoiDungDTO nddto = new NguoiDungDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                nddto = (NguoiDungDTO)Session["ThanhVien"];
                LoadThongTinNguoiDung();
            }
        }

     
        private void LoadThongTinNguoiDung()
        {
            lbTenTV.Text = nddto.HoTen;
            if (nddto.GioiTinh)
            {
                lbGioiTinhTV.Text="Nam";
            }
            else
	        {
                 lbGioiTinhTV.Text = "Nữ";
	        }
            lbNgaySinhTV.Text = nddto.NgaySinh.ToShortDateString();
            lbEmail.Text = nddto.Email;
            lbNgayDangKy.Text = nddto.NgayDangKy.ToShortDateString();
            lbTaiKhoan.Text = nddto.TaiKhoan;
        }

        protected void EditThongTin_Click(object sender, ImageClickEventArgs e)
        {
            fuUpAvatar.Visible = true;
            //drpNgay.Visible = true;
            //drpThang.Visible = true;
            //drpNam.Visible = true;
            drpGioiTinh.Visible = true;
            txtTenTV.Visible = true;
            txtNgaySinh.Visible = true;
            lbGioiTinhTV.Visible = false;
            lbTenTV.Visible = false;
            lbNgaySinhTV.Visible = false;

            divButton.Visible = true;
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            Response.Redirect("TrangChu.aspx");
        }

        private void DisableEdit()
        {
            txtNgaySinh.Visible = false;
            fuUpAvatar.Visible = false;
            //drpNgay.Visible = false;
            //drpThang.Visible = false;
            //drpNam.Visible = false;
            drpGioiTinh.Visible = false;
            txtTenTV.Visible = false;

            lbGioiTinhTV.Visible = true;
            lbTenTV.Visible = true;
            lbNgaySinhTV.Visible = true;

            divButton.Visible = false;
        }
        protected void btnHuy_Click(object sender, EventArgs e)
        {
            DisableEdit();
        }

        protected void btnHienDoiMatKhau_Click(object sender, EventArgs e)
        {
            DoiMatKhau.Visible = true;
            btnHienDoiMatKhau.Visible = false;
        }

        protected void btnHuyDoiMatKhau_Click(object sender, EventArgs e)
        {
            DoiMatKhau.Visible = false;
            btnHienDoiMatKhau.Visible = true;
        }

        protected void btnDongY_Click(object sender, EventArgs e)
        {
            nddto = (NguoiDungDTO)Session["ThanhVien"];
            nddto.GioiTinh = Convert.ToBoolean(drpGioiTinh.SelectedValue);
            nddto.HoTen = txtTenTV.Text;
            nddto.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
            if (fuUpAvatar.FileName.Length>0)
            {
                nddto.Avatar = fuUpAvatar.FileName;
                try
                {
                    fuUpAvatar.SaveAs(Server.MapPath("~/HinhAnh/ThanhVien/" + fuUpAvatar.FileName));
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            if (ndbus.ThayDoiThongTin(nddto))
            {
                Session["NguoiDung"] = nddto;
                LoadThongTinNguoiDung();
                DisableEdit();
            }
        }

        protected void btnDongYDoiMatKhau_Click(object sender, EventArgs e)
        {
            if (txtMatKhauMoi.Text == txtXacNhanMatKhauMoi.Text)
            {
                string taikhoan = nddto.TaiKhoan;
                string matkhaucu  = txtMatKhauCu.Text;
                string matkhaumoi = txtMatKhauMoi.Text;
                if (ndbus.DoiMatKhau(taikhoan,matkhaucu,matkhaumoi))
                {
                    DataTable dt = ndbus.LoginUser(nddto);
                    if (dt.Rows.Count > 0)
                    {
                        DoiMatKhau.Visible = false;
                        ThongBaoDoiMatKhau.Visible = false;
                    }
                }
                else
                {
                    ThongBaoDoiMatKhau.Visible = true;
                }

            }
        }
    }
}